function init(){
	var url = location.search;
	var strs;
	if (url.indexOf("?") != -1) {  
	     var str = url.substr(1);  
	     strs = str.split("&");  
	}
	var postid = strs[0].split("=")[1];
	var pageNumber;
	if(strs.length==1){
		pageNumber = 1;
	}else{
		pageNumber = strs[1];
	}
	initPost(postid);
	initComments(postid);
}

function zhuxiao(){
	$.ajax({
        url: 'user/unsubscribe',
        type: "GET",
        data: {
        }
        ,
        success: function (data) {
        	window.location.href = "login.html";
        }
        ,
        error: function (err) {
            console.log(err);
        }
    }
);
}


function initPost(postid){
	var title;
	var userName;
	var headImg;
	var time;
	var content
	$.ajax({
		url : 'post/getPostByPostid',
		type : "GET",
		async: false,
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				title = data['result']['postInfo']['post']['title'];
				userName = data['result']['postInfo']['user']['userName'];
				headImg = data['result']['postInfo']['user']['headImg'];
				time = data['result']['postInfo']['post']['time'];
				content = data['result']['postInfo']['post']['content'];
				
				$("#postsTitle").text(title);
				$("#date").text(time);
				$("#userName").text(userName);
				$("#userImage").attr("src",headImg);
				document.getElementById("passageText").innerHTML=content;
				
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});	
}


function initComments(postid){
	var comments;
	$.ajax({
		url : 'comment/getCommentOfPost',
		type : "GET",
		async: false,
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				comments = data['result']['comments'];
				var commentsHtml = "";
				for(var i = 0;i<comments.length;i++){
					var comment = comments[i]['comment'];
					var fromUser = comments[i]['fromUser'];
					var toUser = comments[i]['toUser'];
					//如果回复的是帖子
					if(comment['replyType']==1){
						commentsHtml  = commentsHtml + "<div class=\"reply\">"
								+"<a href=\"\">"
                                + "<img class=\"commentUserImage\" id=\"\" src=\""
                                + fromUser['headImg'] 
                                + "\" alt=\"\" width=\"40\" height=\"40\" ></a>"
                                + "<b class=\"commentUserName\" id=\"\"> "
                                + fromUser['userName'] 
								+ ":</b>"
								+"<div class=\"textLine\">"
								+"<span id=\"replyContent"
								+ comment['commentid']
								+"\">"
								+ comment['content']
                        		+ "</span>"
                        		+"</div>"
                        		+"<div class=\"dateAndReply\">"
                        		+"<input type=\"button\" id=\"replyBtn" 
                        		+comment['commentid']
                        		+		"\" class=\"btnReplyComment\" value=\"回复\" onclick=\"showReplyForm(this)\">"
                        		+"<b class=\"commentDate\">" +
                        		comment['time']		
                        		+"</b>"
                        		+"<form id=\"replyForm" +
                        		comment['commentid']
								+"\" method=\"post\" style=\"display: none\" class=\"replyForm\">"
                                +"<textarea  class=\"commentBox\" id=\"replyInput" 
                                +  comment['commentid']
                                +"\"></textarea><br>"
                                +"<input type=\"button\" class=\"commentSubmit\" id=\"replySubmit"
                                +comment['commentid']
								+"\" value=\"发表\" onclick=\"reply(this)\">"
								+"</form>"
                           		+"</div>"
                           		+"</div>";
					}
					//如果回复的是评论
					else{
						commentsHtml  = commentsHtml + "<div class=\"reply\">"
						+"<a href=\"\">"
                        + "<img class=\"commentUserImage\" id=\"\" src=\""
                        + fromUser['headImg'] 
                        + "\" alt=\"\" width=\"40\" height=\"40\" ></a>"
                        + "<b class=\"commentUserName\" id=\"\"> "
                        + fromUser['userName'] 
						+ "</b>"
						+" <b class=\"textReply\">回复&nbsp</b>" 
						+"<a href=\"\">"
						+"<img class=\"commentUserImage\" id=\"\" src=\"" 
						+ toUser['headImg']
						+"\" width=\"40\" height=\"40\"></a>" 
						+"<b class=\"theName\" id=\"\">"
						+ toUser['userName']
						+":</b>"
						+"<div class=\"textLine\">"
						+"<span id=\"replyContent"
						+ comment['commentid']
						+"\">"
						+ comment['content']
                		+ "</span>"
                		+"</div>"
                		+"<div class=\"dateAndReply\">"
                		+"<input type=\"button\" id=\"replyBtn" 
                		+comment['commentid']
                		+		"\" class=\"btnReplyComment\" value=\"回复\" onclick=\"showReplyForm(this)\">"
                		+"<b class=\"commentDate\">" +
                		comment['time']		
                		+"</b>"
                		+"</div>"
                		+"<form id=\"replyForm" 
                		+comment['commentid']
						+"\" method=\"post\" style=\"display: none\" class=\"replyForm\">"
                        +"<textarea  class=\"commentBox\" id=\"replyInput" 
                        + comment['commentid']
                        +"\"></textarea><br>"
                        +"<input type=\"button\" class=\"commentSubmit\" id=\"replySubmit"
                        +comment['commentid']
						+"\" value=\"发表\" onclick=\"reply(this)\">"
						+"</form>"
                		+"</div>";
					}
						  
					}
				document.getElementById("replyPart").innerHTML=commentsHtml;
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});	
}


function showReplyForm(btn){
	var id = btn.id;
	var replyid = id.split("replyBtn")[1];
	var formid = "replyForm" + replyid;
	var form = document.getElementById(formid);
    var state = form.style.display;
    if (state === "block"){
        form.style.display="none";
        btn.value = "回复";
    }else if (state === "none"){
        form.style.display="block";
        btn.value = "取消回复";
    }

}



function reply(btn){
	var id = btn.id;
	var replyid = id.split("replySubmit")[1];
	var inputID = "replyInput" +replyid;
    var input = document.getElementById(inputID);
    var content = input.value;
    $.ajax({
        url: 'comment/makeCommentToComment',
        type: "POST",
        data: {
        	replyid:replyid,
        	content:content
        }
        ,
        success: function (data) {
        	switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				location.reload(true);                            
			case -23:
				alert(data['msg']);
        	}
        }
        ,
        error: function (err) {
            console.log(err);
        }
    }
);
}


function publishComment(){
	//获取postid
	var url = location.search;
	var strs;
	if (url.indexOf("?") != -1) {  
	     var str = url.substr(1);  
	     strs = str.split("&");  
	}
	var postid = strs[0].split("=")[1];
	//获取纯文本内容
	var content = document.getElementById("commentText").value;
	

	$.ajax({
		url: "comment/makeCommentToPost",
		type: "POST",
		data: {
			replyid:postid,
        	content:content
		}
		,
		success: function (data) {
			
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				location.reload(true);                            
			case -23:
				alert(data['msg']);
        	}
        
		}
		,
		error: function (err) {
			console.log(err);
		}
	});
}


